package com.cta.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.web.context.ContextLoader;

import com.cta.entity.ServerConfig;
import com.cta.entity.Trunk;
import com.cta.view.model.ConfigModel;
import com.cta.view.model.DurationLock;
import com.cta.view.model.ProfileModel;

public class CodeLookupUtils {

	public static ServerConfig serverConfig;
	
	public static DurationLock durationLock ;

	private static Map<String, Map<String, ConfigModel>> tariffConfigs = new HashMap<String, Map<String, ConfigModel>>();

	private static Map<String, ProfileModel> profileConfigs = new HashMap<String, ProfileModel>();

	private static Map<String, Trunk> trunks = new HashMap<String, Trunk>();

	/**
	 * key is tariff record id
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, ConfigModel> getConfig(String trunkNo) {
		// get tariff from trunk
		if (trunks.size() > 0) {
			Trunk trunk = trunks.get(trunkNo);
			if (trunk != null) {
				return tariffConfigs.get(String.valueOf(trunk.getConfigId()));
			}
		}
		return null;
	}

	public static ProfileModel getProfile(String key) {
		return profileConfigs.get(key);
	}

	/**
	 * 
	 * @param key
	 *            is id of tariff record
	 * @param trunkConfigs
	 */
	public static void addConfig(String key, Map<String, ConfigModel> tariffConfigs) {
		CodeLookupUtils.tariffConfigs.put(key, tariffConfigs);
	}
	

	/**
	 * 
	 * @param key
	 *            is trunk no of trunk
	 * @param trunkConfigs
	 */
	public static void addTrunk(String key, Trunk trunk) {
		CodeLookupUtils.trunks.put(key, trunk);
	}

	/**
	 * 
	 * @param key
	 *            is pbx no
	 * @param profileConfig
	 */
	public static void addProfileConfig(String key, ProfileModel profileConfig) {
		CodeLookupUtils.profileConfigs.put(key, profileConfig);
	}

	public static synchronized void stop() {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		DefaultMessageListenerContainer exampleProductsMessageListener = ctx.getBean("cdrProductsMessageListener",
				DefaultMessageListenerContainer.class);
		exampleProductsMessageListener.stop();
	}

	public static synchronized void start() {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		DefaultMessageListenerContainer exampleProductsMessageListener = ctx.getBean("cdrProductsMessageListener",
				DefaultMessageListenerContainer.class);
		exampleProductsMessageListener.start();
	}

}
